/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-10-9
 * @Time  : 下午6:08
 * @brief :
 */
var img = __uri('./image/weixin-share.png'),
    util = require("wap_common:widget/ui/libs/util.js").util;

var Share = P.createUi({
    _data:{
        imgUrl:location.protocol+'//'+location.host+img,
        shareTitle:"我发现了一个靠谱的理财产品，推荐给大家！",
        descContent:"赚了钱要想着我哦",
        lineLink:location.protocol+'//'+location.host+'/activity/allindex',
        appMessageTitle:"",
        appMessageDescContent:"",
        appMessageLineLink:"",
        timeLineTitle:"",
        timeLineDescContent:"",
        timeLineLineLink:"",
        appid:"wxba62c5e4899dd28a",
        isAutoGetData:true,
        onSendFriend:function(){},
        onSendFriends:function(){},
        onSend:function(){},
        onSendWeibo:function(){}
    },

    _create:function(){
        var me = this;
        this.trigger('create');
        document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
            // 发送给好友
            WeixinJSBridge.on('menu:share:appmessage', function(argv){
                var title = me.data('appMessageTitle')?me.data('appMessageTitle'):me.data('shareTitle'),
                    desc = me.data('appMessageDescContent')?me.data('appMessageDescContent'):me.data('descContent'),
                    link =  me.data('appMessageLineLink')?me.data('appMessageLineLink'):me.data('lineLink');

                WeixinJSBridge.invoke('sendAppMessage',{
                    "appid": me.data("appid"),
                    "img_url": me.data("imgUrl"),
                    "img_width": "640",
                    "img_height": "640",
                    "link":link ,
                    "desc": desc,
                    "title":title
                }, function(res) {
                    me.trigger("sendFriend",res);
                    me.trigger("send",res);
                })
            });

            // 分享到朋友圈
            WeixinJSBridge.on('menu:share:timeline', function(argv){
                var title = me.data('timeLineTitle')?me.data('timeLineTitle'):me.data('shareTitle'),
                    desc = me.data('timeLineDescContent')?me.data('timeLineDescContent'):me.data('descContent'),
                    link =  me.data('timeLineLineLink')?me.data('timeLineLineLink'):me.data('lineLink');

                WeixinJSBridge.invoke('shareTimeline',{
                    "img_url": me.data("imgUrl"),
                    "img_width": "640",
                    "img_height": "640",
                    "link":link,
                    "desc": desc,
                    "title":title
                }, function(res) {
                    me.trigger("sendFriends",res);
                    me.trigger("send",res);
                });
            });

            //分享到微博
            function shareWeibo() {
                WeixinJSBridge.invoke('shareWeibo',{
                    "content": me.data("descContent"),
                    "url": me.data("imgUrl")
                }, function(res) {
                    me.trigger("sendWeibo",res);
                    me.trigger("send",res);
                });
            }
        }, false);
        me.trigger("beforeGetData");
    },

    _init:function(){
        this.on('send',function(res){

        });
    }
});

$.extend(exports,{
    Share:Share,
    share:function(option){
        return new Share(option);
    }
});